package cn.bdqn.t354.controller;

import cn.bdqn.t354.pojo.Right;
import cn.bdqn.t354.service.RightService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/right")
public class RightController {
    @Resource
    private RightService rightService;

    @RequestMapping("/reload")
    public String reload(){
        rightService.reloadFilterChainDefinitions();
        return "redirect:/main";
    }

    /**
     * 列表展示
     * @param rightText
     * @param rightType
     * @param pageIndex
     * @param model
     * @return
     */
    @RequestMapping("/list")
    public String list(String rightText,String rightType,
                       @RequestParam(defaultValue = "1") Integer pageIndex,
                       Model model) {
        //设置每页显示条数
        int pageSize = 3;

        //查询每页显示的数据
        IPage<Right> rightPage =
                rightService.getRightsByPage(rightText,rightType,pageIndex, pageSize);
        model.addAttribute("rightPage", rightPage);

        //查询条件 回显
        model.addAttribute("rightText", rightText);
        model.addAttribute("rightType", rightType);

        return "right/list";
    }

    /**
     * 跳转到新增页面
     *
     * @param model
     * @return
     */
    @RequestMapping("/add")
    public String add(Model model) {
        return "right/add";
    }

    /**
     * 新增操作
     *
     * @param right
     * @return
     */
    @RequestMapping("/save")
    public String save(Right right) {
        try {
            rightService.addRight(right);
            //跳转到资源列表页面
            return "redirect:/right/list";
        } catch (Exception e) {
            e.printStackTrace();
            //跳转到资源新增页面
            return "redirect:/right/add";
        }
    }

    /**
     * 跳转到修改页面
     *
     * @param rightCode
     * @param model
     * @return
     */
    @RequestMapping("/edit")
    public String edit(String rightCode, Model model) {
        //1.根据id查询资源信息
        Right right = rightService.getRight(rightCode);
        model.addAttribute("right", right);

        //父级资源列表
        List<Right> rightList=null;
        if(right.getRightType().equals("Document")){
            rightList = rightService.findRightsByRightType("Folder");
        }else if(right.getRightType().equals("Url")){
            rightList=rightService.findRightsByRightType("Document");
        }
        model.addAttribute("rightList",rightList);

        return "right/edit";
    }

    /**
     * 修改操作
     * @param right
     * @return
     */
    @RequestMapping("/update")
    public String update(Right right) {
        try {
            rightService.updateRight(right);
            //跳转到资源列表页面
            return "redirect:/right/list";
        } catch (Exception e) {
            e.printStackTrace();
            //跳转到资源修改页面
            return "redirect:/right/edit";
        }
    }

    /**
     * 删除操作
     *
     * @param rightCode
     * @return
     */
    @RequestMapping("/delete")
    @ResponseBody
    public Map<String, Object> delete(String rightCode) {
        Map<String, Object> map = new HashMap<>();
        try {
            rightService.deleteRight(rightCode);
            map.put("delResult", true);
        } catch (Exception e) {
            e.printStackTrace();
            map.put("delResult", false);
        }
        return map;
    }

    @RequestMapping("/findAllRights")
    @ResponseBody
    public List<Right> findAllRights(String rightType) {
        List<Right> rights = rightService.findRightsByRightType(rightType);
        return rights;
    }
}
